package com.zhidisoft.crm.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zhidisoft.crm.entity.SysUser;
import com.zhidisoft.crm.vo.SysUserVo;
/**
 * 用户的增删改查
 * @author Administrator
 *
 */
public interface ISysUserMapper {
	
	
	/**
	 * 添加用户
	 * @param user
	 */
	@Insert("insert into sys_user(id,username,password)"
	 + " values(#{id},#{username},#{password})")
	void save(SysUserVo user);

	
	/**
	 * 分页查询
	 * @param username
	 * @return
	 */
//	mysql
//	@Select("select su.id,su.username,sr.role_name,su.phone,su.create_time from"
//			+ " sys_user su left join sys_user_role sur on "
//			+ "su.id=sur.user_id left join sys_role sr on "
//			+ "sr.id=sur.role_id order by su.create_time "
//			+ "desc limit #{startIndex},#{pageSize}")
	//orcle
	@Select("SELECT * FROM(SELECT A.*,ROWNUM RN "
			+ "FROM (SELECT su.id,su.username,sr.role_name,su.create_time FROM SYS_USER "
			+ "su left join sys_user_role sur on "
			+ "su.id=sur.user_id left join sys_role sr on "
			+ "sr.id=sur.role_id order by su.create_time) A "
			+ "WHERE ROWNUM<=#{endIndex}) TL WHERE RN >#{startIndex}")
	List<SysUserVo> pageList(@Param("startIndex") Integer startIndex,@Param("endIndex") Integer endIndex);

	
	/**
	 * 查询总条数
	 * @return
	 */
	@Select("select count(id) from sys_user")
	long totalCount();
	
	
	/**
	 * 根据id查询一条数据
	 * @param id
	 * @return
	 */
	@Select("select * from sys_user where id=#{id}")
	SysUserVo findId(String id);
	
	
	/**
	 * 根据id删除用户
	 * @param id
	 */
	@Delete("delete from sys_user where id=#{id}")
	void delete(String id);
	
	
	/**
	 * 根据id修改用户信息
	 * @param user
	 */
	@Update("update sys_user set username=#{username} "
			+ "where id=#{id}")
	void update(SysUser user);

	@Select("select * from sys_user where username=#{username}")
	SysUserVo selectByName(String username);
	
	
}
